﻿@page "/Production/CardInfos"
@using BootstrapAdmin.DataAccess.Models
@using BootstrapAdmin.Web.Core.Services.Production;
@using BootstrapAdmin.Web.Validators;
@inject ICardInfoService cardInfoService

<AdminTable TItem="CardInfo" IsMultipleSelect="true" IsPagination="true" PageItemsSource="PageItemsSource"
    ShowToolbar="true" ShowSearch="true" SearchMode="SearchMode.Top" SearchModel="@SearchModel" ShowExportButton="true"
    OnQueryAsync="@OnQueryAsync" OnSaveAsync="@OnSaveAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.CardNo" Searchable="true"></TableColumn>
        <TableColumn @bind-Field="@context.PLevel" Searchable="true"></TableColumn>
        <TableColumn @bind-Field="@context.Owner"></TableColumn>
        <TableColumn @bind-Field="@context.Description"></TableColumn>
        <TableColumn @bind-Field="@context.IsEnabled"></TableColumn>
        <TableColumn @bind-Field="@context.CreatedAt"></TableColumn>
    </TableColumns>
    <EditTemplate>
        <div class="row g-3 form-inline">
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.CardNo" IsDisabled="GetDisabled(context.Id)" ValidateRules="Rules" 
                    placeholder="不可为空，16字以内" maxlength="16" IsTrim="true" />
            </div>
            <div class="col-12 col-sm-6">
                 <Select @bind-Value="@context.PLevel" />
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Owner" maxlength="32" />
            </div>
            <div class="col-12 col-sm-6">
                <BootstrapInput @bind-Value="@context.Description" maxlength="64" />
            </div>
            <div class="col-12 col-sm-6">
                <Switch @bind-Value="@context.IsEnabled" ShowInnerText="true" />
            </div>
        </div>
    </EditTemplate>
</AdminTable>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 20, 50, 100 };

    private CardInfo SearchModel = new();

    private List<IValidator> Rules { get; } = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Rules.Add(new CardNoValidator(cardInfoService));
    }

    private async Task<QueryData<CardInfo>> OnQueryAsync(QueryPageOptions options)
    {
        var ret = new QueryData<CardInfo>()
        {
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true
        };

        var (items, count) = await cardInfoService.GetPagedAsync(SearchModel, options.PageIndex, options.PageItems);
        ret.Items = items;
        ret.TotalCount = count;

        return ret;
    }

    private async Task<bool> OnSaveAsync(CardInfo cardInfo, ItemChangedType itemChangedType)
    {
        switch (itemChangedType)
        {
            case ItemChangedType.Add:
                await cardInfoService.AddCardInfoAsync(cardInfo);
                break;
            case ItemChangedType.Update:
                await cardInfoService.UpdateCardInfoAsync(cardInfo);
                break;
            default:
                break;
        }
        
        return true;
    }

    private static bool GetDisabled(int id) => id > 0;
}
